package com.market.cash.utils.dialog

import android.annotation.SuppressLint
import android.content.Context
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import com.market.cash.CashRootApps
import com.market.cash.R
import com.market.cash.databinding.CashUserDataClearDialogBinding
import com.market.cash.pro.ProDialog
import com.market.cash.utils.event.CashRootEventHelper
import com.market.cash.utils.event.CashRootEventType
import com.market.cash.utils.thread.TimingThread

/**
 * CashRoot
 * @Author wwdeng
 * @CreateTime: 2024/10/28 15:50
 * @Annotation：清空用户数据dialog
 */
class CashRootUserDataClearDialog: ProDialog, TimingThread.ITimingThreadListener {

    private lateinit var binding: CashUserDataClearDialogBinding
    private var mCallback: IDialogCallback? = null

    //倒计时
    private var countdown = 10
    private var timeThread: TimingThread? = null

    override fun initWindow() {
        val params = window!!.attributes
        params.width = WindowManager.LayoutParams.MATCH_PARENT
        params.height = WindowManager.LayoutParams.WRAP_CONTENT
        window!!.attributes = params
        window!!.setWindowAnimations(R.style.dialog_slide_anim) //修改动画样式
        window!!.setGravity(Gravity.BOTTOM)
    }

    constructor(context: Context) : super(context) {}
    constructor(context: Context, callback: IDialogCallback?) : super(context) {
        mCallback = callback
    }

    /**
     * 设置点击回调
     */
    fun setOnClick(callback: IDialogCallback){
        this.mCallback = callback
    }

    override fun getRootView(): View {
        binding = CashUserDataClearDialogBinding.inflate(LayoutInflater.from(context))
        return binding.root
    }

    override fun initView() {

    }

    override fun initListener() {
        setOnClickListener(binding.imgClose)
        setOnClickListener(binding.btnReturn)
        setOnClickListener(binding.btnConfirm)
    }

    override fun initData() {

    }

    override fun onViewClick(v: View?) {
        dismiss()
        when(v!!.id){
            R.id.btnConfirm -> {
                if(mCallback != null){
                    mCallback!!.onCashRootConfirmClick()
                }
            }
        }
    }

    interface IDialogCallback {
        //按钮点击
        fun onCashRootConfirmClick()
    }

    /**
     * 启动倒计时
     */
    @SuppressLint("SetTextI18n")
    private fun startTimeThread() {
        if (timeThread != null) {
            timeThread!!.stopThread()
        }
        countdown = 10
        checkCashRootConfirmStatus()
        if (timeThread == null) {
            timeThread = TimingThread()
            timeThread!!.setThreadDelayMillis(1000)
            timeThread!!.setLoadListener(this)
        }
        timeThread!!.startThread()
    }

    override fun onLoading() {
        if (countdown <= 0) {
            checkCashRootConfirmStatus()
            countdown = 10
            timeThread!!.stopThread()
            return
        }
        countdown--
        checkCashRootConfirmStatus()
    }

    @SuppressLint("SetTextI18n")
    private fun checkCashRootConfirmStatus(){
        if (countdown <= 0){
            binding.btnConfirm.isEnabled = true
            binding.btnConfirm.text = CashRootApps.app.getString(R.string.btn_confirm)
        } else {
            binding.btnConfirm.isEnabled = false
            binding.btnConfirm.text = "${CashRootApps.app.getString(R.string.btn_confirm)}(${countdown}s)"
        }
    }

    override fun onStart() {
        super.onStart()
        startTimeThread()
        CashRootEventHelper.addCashRootRequestEventAction(CashRootEventType.DIALOG.CASHROOT_DIALOG_USER_DATA_CLEAR_SHOW)
    }

    override fun dismiss() {
        super.dismiss()
        if (timeThread != null) {
            timeThread!!.stopThread()
            timeThread = null
        }
        CashRootEventHelper.addCashRootRequestEventAction(CashRootEventType.DIALOG.CASHROOT_DIALOG_USER_DATA_CLEAR_DISMISS)
    }
}